package me.shingohu.man.di.module;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.EventBusBuilder;
import org.greenrobot.eventbus.meta.SubscriberInfoIndex;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import me.shingohu.man.di.config.EventBusConfig;

/**
 * Created by hujie on 17/4/14.
 */

@Module
public class EventBusModule {


    @Singleton
    @Provides
    EventBus provideEventBus(EventBusConfig config) {
        EventBusBuilder builder = EventBus.builder()
                .logNoSubscriberMessages(config.isLogNoSubscriberMessages())
                .sendNoSubscriberEvent(config.isSendNoSubscriberEvent())
                .ignoreGeneratedIndex(config.isIgnoreGeneratedIndex())
                .throwSubscriberException(config.isThrowSubscriberException());
        if (config.getInfoIndex() != null) {
            builder.addIndex(config.getInfoIndex());
        }
        return builder.installDefaultEventBus();
    }

    @Singleton
    @Provides
    EventBusConfig provideEventBusConfig(SubscriberInfoIndex subscriberInfoIndex) {
        EventBusConfig config = new EventBusConfig();
        if (subscriberInfoIndex != null) {
            config.setInfoIndex(subscriberInfoIndex);
        }
        return config;
    }
}
